package cn.itcast.controller;


import cn.itcast.common.R;
import cn.itcast.dto.DishDto;
import cn.itcast.dto.SetmealDto;
import cn.itcast.entity.Category;
import cn.itcast.entity.Dish;
import cn.itcast.entity.Setmeal;
import cn.itcast.entity.SetmealDish;
import cn.itcast.service.CategoryService;
import cn.itcast.service.DishService;
import cn.itcast.service.SetmealDishService;
import cn.itcast.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save (@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> getAll(int page, int pageSize, String name){
        //1 获取分页对象
        Page<Setmeal> page1 = new Page<>(page,pageSize);
    /*    //2 基本的分页查询  不能查询到套餐分类
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.orderByAsc(Setmeal::getCreateTime);
        setmealService.page(page1,setmealLambdaQueryWrapper);
        return R.success(page1);*/
        //3 对象拷贝  双表联查
        Page<SetmealDto> page2 = new Page<>();
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        setmealLambdaQueryWrapper.orderByAsc(Setmeal::getCreateTime);
        setmealService.page(page1,setmealLambdaQueryWrapper);
        //!!!!!!!!!!!!!  排除 "recode"  这个需要自己处理
        BeanUtils.copyProperties(page1,page2,"recode");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> collect = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            String name1 = byId.getName();
            setmealDto.setCategoryName(name1);
            return setmealDto;
        }).collect(Collectors.toList());

        page2.setRecords(collect);


        return R.success(page2);

    }

    /**
     * 套餐起售禁售修改
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateByIds(@PathVariable int status,@RequestParam List<Long> ids) {
        for (Long id : ids) {
            Setmeal byId = setmealService.getById(id);
            byId.setStatus(status);
            setmealService.updateById(byId);
        }
        return R.success("修改成功");
    }

    /**
     * 修改时数据回显
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<SetmealDto> getWithDish(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.reSee(id);
        return R.success(setmealDto);
    }


    /**
     * 修改套餐
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<SetmealDto> listR(@PathVariable long id){

        SetmealDto setmealDto = setmealService.reSee(id);
        List<Dish> dishList = dishService.list();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            for (Dish dish : dishList) {
                if(dish.getId() .equals(setmealDish.getDishId())){
                    setmealDish.setImage(dish.getImage());
                }
            }
        }
        return R.success(setmealDto);
    }

}

